from goods.models import GoodsVisitCount
from datetime import date, timedelta


def counter_days(category_id, days):
    '''封装商品类别访问量'''
    # 1、获取当前日期
    now_date = date.today()
    # 2、获取n天前的日期
    start_date = now_date - timedelta(days=days - 1)

    # 构建列表数据
    data_list = []
    for i in range(days):
        # 获取起始时间
        index_date = start_date + timedelta(days=i)
        # 获取第二天实现
        next_date = index_date + timedelta(days=1)
        count = GoodsVisitCount.objects.filter(date__gte=index_date,
                                               date__lt=next_date,
                                               category_id=category_id).count()
        data_list.append({'count': count, 'date': index_date})
    # 3、返回结果
    return data_list
